package com.loyalove.demo.kotlinboot.controller

import com.loyalove.demo.kotlinboot.model.UserVO
import com.loyalove.demo.kotlinboot.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
class UserController {

    @Autowired
    lateinit var userRepository: UserRepository

    @GetMapping("/users")
    fun find(userVO: UserVO): List<UserVO>? {
        return userRepository.findAll()
    }

    @PostMapping("/users")
    fun post(userVO: UserVO): UserVO? {
        return userRepository.insert(userVO)
    }

    @GetMapping("/users/{id}")
    fun get(@PathVariable("id") id: Long): UserVO? {
        return userRepository.findById(id).get()
    }

    @PutMapping("/users/{id}")
    fun put(@PathVariable("id") id: Long, userVO: UserVO): UserVO? {
        userVO.id = id
        return userRepository.save(userVO)
    }

    @DeleteMapping("/users/{id}")
    fun del(@PathVariable("id") id: Long): Unit? {
        return userRepository.deleteById(id)
    }
}